<template>
    <div class="app-container">
      <el-form :model="queryParams" ref="queryRef" :inline="true" v-show="showSearch">
        <el-form-item label="公告标题" prop="title">
          <el-input v-model="queryParams.title" placeholder="请输入公告标题" clearable style="width: 200px" @keyup.enter="handleQuery" />
        </el-form-item>
        <el-form-item label="公告类别" prop="category">
          <el-select v-model="queryParams.category" placeholder="选择公告类别" clearable style="width: 200px">
            <el-option label="行政通知" :value="0" />
            <el-option label="活动公告" :value="1" />
            <el-option label="政策信息" :value="2" />
            <el-option label="其他" :value="3" />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" icon="Search" @click="handleQuery">搜索</el-button>
          <el-button icon="Refresh" @click="resetQuery">重置</el-button>
        </el-form-item>
      </el-form>
  
      <el-row :gutter="10" class="mb8">
        <el-col :span="1.5">
          <el-button type="primary" plain icon="Plus" @click="handleAdd">新增公告</el-button>
        </el-col>
      </el-row>
  
      <el-table v-if="refreshTable" v-loading="loading" :data="noticeList" row-key="id">
        <el-table-column prop="title" label="公告标题" width="200"></el-table-column>
        <el-table-column prop="date" label="发布日期" width="200"></el-table-column>
        <el-table-column prop="author" label="发布者" width="150"></el-table-column>
        <el-table-column prop="category" label="公告类别" width="150">
          <template #default="scope">
            <span>{{ getCategoryLabel(scope.row.category) }}</span>
          </template>
        </el-table-column>
        <el-table-column prop="expirationDate" label="过期日期" width="200"></el-table-column>
        <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
          <template #default="scope">
            <el-button link type="primary" icon="Edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button link type="primary" icon="Delete" @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
  
      <el-dialog :title="title" v-model="open" width="600px" append-to-body>
        <el-form ref="noticeRef" :model="form" :rules="rules" label-width="80px">
          <el-row>
            <el-col :span="12">
              <el-form-item label="公告标题" prop="title">
                <el-input v-model="form.title" placeholder="请输入公告标题" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="发布者" prop="author">
                <el-input v-model="form.author" placeholder="请输入发布者" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="公告类别" prop="category">
                <el-select v-model="form.category" placeholder="选择公告类别">
                  <el-option label="行政通知" :value="0" />
                  <el-option label="活动公告" :value="1" />
                  <el-option label="政策信息" :value="2" />
                  <el-option label="其他" :value="3" />
                </el-select>
              </el-form-item>
            </el-col>
            <el-col :span="24">
              <el-form-item label="公告内容" prop="content">
                <el-input type="textarea" v-model="form.content" placeholder="请输入公告内容" />
              </el-form-item>
            </el-col>
            <el-col :span="12">
              <el-form-item label="过期日期" prop="expirationDate">
                <el-input type="datetime-local" v-model="form.expirationDate" />
              </el-form-item>
            </el-col>
          </el-row>
        </el-form>
        <template #footer>
          <div class="dialog-footer">
            <el-button type="primary" @click="submitForm">确 定</el-button>
            <el-button @click="cancel">取 消</el-button>
          </div>
        </template>
      </el-dialog>
    </div>
  </template>
  
  <script setup name="NoticeList">
  import { ref, reactive } from 'vue';
  import { listNotices, addNotice, updateNotice, deleteNotice } from "@/api/pages/notice";
  
  const loading = ref(true);
  const open = ref(false);
  const showSearch = ref(true);
  const title = ref("");
  const refreshTable = ref(true);
  const noticeList = ref([]);
  const form = reactive({
    id: undefined,
    title: "",
    content: "",
    date: new Date(),
    author: "",
    category: 0,
    expirationDate: new Date()
  });
  const queryParams = reactive({
    title: "",
    category: undefined
  });
  const rules = {
    title: [{ required: true, message: "公告标题不能为空", trigger: "blur" }],
    content: [{ required: true, message: "公告内容不能为空", trigger: "blur" }],
  };
  
  // 查询公告列表
  const getList = () => {
    loading.value = true;
    listNotices(queryParams).then(response => {
      noticeList.value = response.data;
      loading.value = false;
    });
  };
  
  // 取消按钮
  const cancel = () => {
    open.value = false;
    reset();
  };
  
  // 重置表单
  const reset = () => {
    Object.assign(form, {
      id: undefined,
      title: "",
      content: "",
      date: new Date(),
      author: "",
      category: 0,
      expirationDate: new Date()
    });
  };
  
  // 搜索按钮操作
  const handleQuery = () => {
    getList();
  };
  
  // 重置按钮操作
  const resetQuery = () => {
    Object.assign(queryParams, {
      title: "",
      category: undefined
    });
    getList();
  };
  
  // 新增按钮操作
  const handleAdd = () => {
    reset();
    open.value = true;
    title.value = "新增公告";
  };
  
  // 修改按钮操作
  const handleUpdate = (row) => {
    Object.assign(form, row);
    open.value = true;
    title.value = "修改公告";
  };
  
  // 提交按钮
  const submitForm = () => {
    const apiCall = form.id ? updateNotice : addNotice;
    apiCall(form).then(() => {
      getList();
      open.value = false;
    });
  };
  
  // 删除按钮操作
  const handleDelete = (row) => {
    deleteNotice(row.id).then(() => {
      getList();
    });
  };
  
  // 获取公告类别标签
  const getCategoryLabel = (category) => {
    switch (category) {
      case 0: return '行政通知';
      case 1: return '活动公告';
      case 2: return '政策信息';
      case 3: return '其他';
      default: return '';
    }
  };
  
  // 初始化公告列表
  getList();
  </script>
  
  <style scoped>
  .app-container {
    padding: 20px;
  }
  .mb8 {
    margin-bottom: 8px;
  }
  .dialog-footer {
    text-align: right;
  }
  </style>